package com.guw.AWT;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestWindow {
    public static void main(String[] args) {
        new WindowFrame();

    }
}

class WindowFrame extends Frame {
    public WindowFrame() {
        setBackground(Color.yellow);
        setBounds(100, 100, 300, 300);
        setVisible(true);

        //加入窗口关闭功能的内部类,推荐这种写法
        this.addWindowListener(
                new WindowAdapter() {
                    //关闭窗口
                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.out.println("系统退出!");
                        System.exit(0);
                    }

                    //激活窗口
                    @Override
                    public void windowActivated(WindowEvent e) {
                        WindowFrame source = (WindowFrame) e.getSource();
                        source.setTitle("窗口被激活了");
                        System.out.println("windowActivated");
                    }
                }


        );


        //这种写法太复杂了
    /*class MyWindowListener extends WindowAdapter{

        @Override
        public void windowClosing(WindowEvent e) {
            //正常退出0,非正常退出1
            System.exit(0);
            //隐藏窗口
            //setVisible(false);
        }

        @Override
        public void windowActivated(WindowEvent e) {
            WindowFrame source = (WindowFrame) e.getSource();
            source.setTitle("窗口被激活了");
            System.out.println("windowActivated");
        }
    }*/


    }
}